package com.zznode.client.wlanpolling.test;

import java.util.HashMap;
import java.util.Map;

import android.app.Activity;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.zznode.client.wlanpolling.R;
import com.zznode.client.wlanpolling.WLANPollingApplication;
import com.zznode.client.wlanpolling.model.TestResultConstants;
import com.zznode.client.wlanpolling.model.WLANPollManager;
import com.zznode.client.wlanpolling.utils.AbstractMessageUpdater;
import com.zznode.client.wlanpolling.utils.IMessageUpdater;
import com.zznode.client.wlanpolling.utils.PingTask;
import com.zznode.client.wlanpolling.utils.PingUtils;

public class ManualPingActivity extends Activity {

	private EditText tv_pingaddress = null;
	private TextView tv_manualping = null;
	private TextView tv_manuallost = null;
	private TextView tv_manualdelay = null;
	private TextView tv_manualpkglost = null;
	private TextView tv_manualmindelay = null;
	private TextView tv_manualmaxdelay = null;
	
	private AsyncTask<String, String, String> manualPingTask = null;
	
	private WLANPollingApplication application;
	
	private String manualPingParams;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		application = (WLANPollingApplication)getApplication();
		setContentView(R.layout.manualping);
		Button btn = (Button)this.findViewById(R.id.manualping_btn);
		btn.setText("测试");
		btn.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View v) {
				startTestManualPing();
			}
			
		});
		
		tv_pingaddress = (EditText)this.findViewById(R.id.apswitch_pingaddress);
		tv_manualping = (TextView)this.findViewById(R.id.apswitch_manualping);
		tv_manualdelay = (TextView)this.findViewById(R.id.apswitch_manualdelay);
		tv_manuallost = (TextView)this.findViewById(R.id.apswitch_manuallost);
		tv_manualpkglost = (TextView)this.findViewById(R.id.apswitch_manualpkglost);
		tv_manualmindelay = (TextView)this.findViewById(R.id.apswitch_manualmindelay);
		tv_manualmaxdelay = (TextView)this.findViewById(R.id.apswitch_manualmaxdelay);
		
		btn = (Button)this.findViewById(R.id.manualping_save);
		btn.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View v) {
				saveAction();
			}
			
		});
		new Handler().postDelayed(new Runnable(){
			public void run(){
				InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
				imm.hideSoftInputFromWindow(tv_pingaddress.getWindowToken(), InputMethodManager.HIDE_IMPLICIT_ONLY);
			}
		}, 200);		
	}
	
	private String replaceStr(String str1, String str2) {
		return str1.replace(str2, "");
	}
	
	protected void saveAction(){
		String apMac = PingUtils.clearMacAdress(application.getContext().getCurrentWifi().getBSSID());

		Map<String, Object> values = new HashMap<String, Object>();
		
		values.put(TestResultConstants.MANUALPINGDELAY, replaceStr(tv_manualdelay.getText().toString(), "ms"));
		values.put(TestResultConstants.MANUALPINGPACKAGE, tv_manuallost.getText().toString());
		values.put(TestResultConstants.MANUALPINGPARAMS, manualPingParams);
		values.put(TestResultConstants.SSID, application.getContext().getCurrentWifi().getSSID());
		
		WLANPollManager.getWLANPollManager().updateTestResult(application.getApplicationContext(), 
				apMac,
				values);
		
		Toast.makeText(this, "成功保存", Toast.LENGTH_SHORT).show();
	}
	
	private void startTestManualPing(){
		tv_manualping.setText("");
		String input = tv_pingaddress.getText().toString();
		
		if(input == null || "".equals(input)){
			Toast.makeText(this, "请输入地址", Toast.LENGTH_LONG).show();
			return;
		}
		
		//保存手工Ping的参数
		manualPingParams = input;
		TextView textView = (TextView)findViewById(R.id.apswitch_params);
		textView.setText(manualPingParams);
		
		//设置监听Ping的结果
		IMessageUpdater i1 = new AbstractMessageUpdater(this){

			@Override
			public void messageUpdated(String message) {
				tv_manualping.append("\n" + message);
			}
			
		};
		
		IMessageUpdater i2 = new AbstractMessageUpdater(this){

			@Override
			public void messageUpdated(String message) {
				tv_manualdelay.setText(message + "ms");
			}
			
		};
		IMessageUpdater i3 = new AbstractMessageUpdater(this){

			@Override
			public void messageUpdated(String message) {
				tv_manuallost.setText(message + "%");
			}
			
		};
		IMessageUpdater i4 = new AbstractMessageUpdater(this){

			@Override
			public void messageUpdated(String message) {
				tv_manualpkglost.setText(message);
			}
			
		};
		IMessageUpdater i5 = new AbstractMessageUpdater(this){

			@Override
			public void messageUpdated(String message) {
				tv_manualmindelay.setText(message + "ms");
			}
			
		};
		IMessageUpdater i6 = new AbstractMessageUpdater(this){

			@Override
			public void messageUpdated(String message) {
				tv_manualmaxdelay.setText(message + "ms");
			}
			
		};
		int index = input.lastIndexOf(" ");
		String ip = null;
		String params = null;
		if(index != -1){
			ip = input.substring(index + 1, input.length());
			params = input.substring(0, index);
		} else {
			ip = input;
			params = "";
		}
		manualPingTask = new PingTask(new IMessageUpdater[]{i1, i2, i3, i4, i5, i6}, null).execute(new String[]{params, ip});
	}
	
	@Override
	public void onBackPressed() {
//		unregisterReceiver(receiverWifi);
		clearThreads();
		super.onBackPressed();
	}
	
	protected void clearThreads(){
		if(manualPingTask != null && !manualPingTask.isCancelled()){
			manualPingTask.cancel(true);
		}
	}
}
